package com.xing.zrtest;

import java.io.PrintStream;

/**
 * 分库分表 sql生成工具
 *
 * @author lin
 * @data 2021/4/29 15:11
 */
public class GetSqlUtil {

    public static String dbSign = "-";
    public static String tableSign = "_";

    public static String getAlterSql(String db, String table, int fromDb, int endDb, int fromTable, int endTable, String exeSql) {
        StringBuffer sqlResult = new StringBuffer();
        for (int i = fromDb; i <= endDb; i++) {
            for (int j = fromTable; j <= endTable; j++) {
                sqlResult.append("ALTER TABLE `").append(db).append(dbSign).append(i).append("`.`").append(table).append(tableSign).append(j).append("`").append("\r\n").append(exeSql).append("\r\n").append("\r\n");
            }
        }
        return sqlResult.toString();
    }

    public static String getCreateSql(String db, String table, int fromDb, int endDb, int fromTable, int endTable, String exeSql) {
        StringBuffer sqlResult = new StringBuffer();
        for (int i = fromDb; i <= endDb; i++) {
            for (int j = fromTable; j <= endTable; j++) {
                sqlResult.append("CREATE TABLE `").append(db).append(dbSign).append(i).append("`.`").append(table).append(tableSign).append(j).append("`").append(exeSql).append("\r\n").append("\r\n");
            }
        }
        return sqlResult.toString();
    }

    public static void main(String args[]) {
        String db = "mall-order";
        String table = "pay_order";
        int fromDb = 0;
        int endDb = 5;
        int fromTable = 0;
        int endTable = 127;
        String exeSql = "ADD INDEX tid_idx(`tid`);";
        String sql = getAlterSql(db, table, fromDb, endDb, fromTable, endTable, exeSql);
        //System.out.println(sql);
        addFile(sql);
    }

    public static boolean addFile(String string) {
        String path = "/Users/ljx/Downloads/sql.txt";
        PrintStream stream = null;
        try {
            stream = new PrintStream(path);//写入的文件path
            stream.print(string);//写入的字符串
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

//    public static void main(String args[]) {
//        String db = "mall-order";
//        String table = "pay";
//        int fromDb = 0;
//        int endDb = 5;
//        int fromTable = 0;
//        int endTable = 127;

//        String exeSql = "(\n" +
//                "  `id` bigint(20) NOT NULL COMMENT '主键(雪花算法生成)',\n" +
//                "  `uid` varchar(36) NOT NULL DEFAULT '' COMMENT '会员标识',\n" +
//                "  `refund_order_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '退款订单号',\n" +
//                "  `type` tinyint(4) NOT NULL DEFAULT '10' COMMENT '凭证类型(10发货物流凭证、20退款凭证)',\n" +
//                "  `voucher_pic` varchar(255) NOT NULL DEFAULT '' COMMENT '凭证图片地址,用逗号隔开',\n" +
//                "  `is_del` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除. 0未删除，1已删除',\n" +
//                "  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
//                "  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后一次更新时间',\n" +
//                "  PRIMARY KEY (`id`) USING BTREE,\n" +
//                "  KEY `uid_idx` (`uid`) USING BTREE,\n" +
//                "  KEY `refund_order_no_idx` (`refund_order_id`) USING BTREE\n" +
//                ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='退款单凭证表。一个退款单或者退款物流最多3张凭证图片';";

//        String sql = getAlterSql(db, table, fromDb, endDb, fromTable, endTable, exeSql);
//        String sql = getCreateSql(db, table, fromDb, endDb, fromTable, endTable, exeSql);
//        System.out.println(sql);

    //addFile(sql);
//    }
}